<template>
    <div class="container addmusic loading">
        <main  class="table-div">
            <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
              <el-form-item label="祝福名称" prop="name">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
              <el-form-item label="祝福内容" prop="content">
                <el-input type="textarea" v-model="form.content"></el-input>
              </el-form-item>
               <el-form-item label="绑定模版">
                <el-transfer
                  filterable
                  :filter-method="findTemplate"
                  filter-placeholder="搜索模版"
                  :titles="['模版列表', '已选中']"
                  :button-texts="['解除', '选中']"
                  v-model="checkedTempData"
                  :data="tempData">
                  <span slot-scope="{ option }">{{ option.label }}</span>
                </el-transfer>
              </el-form-item>
              <el-form-item>
                <el-button v-if="!id" type="primary" @click="submitAdd('ruleForm')">确认添加</el-button>
                <el-button v-if="id" type="primary" @click="submitEdit('ruleForm')">确认修改</el-button>
              </el-form-item>
            </el-form>
        </main>
    </div>
</template>

<script>
import { addBless, getBlessDetail, editBless } from '@/api/bless'
import { getTemplateByBlessingsId } from '@/api/template'

import { getTemplateList } from '@/api/template'
export default {
  data () {
    return {
      id: '',
      form: {
        name: '',
        content: '',
        ids: ''
      },
      tempAjax: {
        pageNum: 1,
        pageSize: 10000,
        findContent: ''
      },
      tempData: [],
      checkedTempData: [],
      rules: {
        name: [
          { required: true, message: '请输入祝福语名称', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入祝福语内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    findTemplate (query, item) {
      if (query) {
        return item.label.indexOf(query) > -1
      } else {
        return item
      }
    },
    getTempData () {
      getTemplateList(this.tempAjax).then(data => {
        if (data.result === 1) {
          this.tempData = Object.entries(data.data.list).map(item => {
            return {
              label: item[1].templatename,
              key: item[1].id
            }
          })
        } else {
          this.tempData = []
        }
      })
    },
    getDetails (id) {
      getBlessDetail({ id: id }).then(data => {
        if (data.result === 1) {
          this.form.name = data.data.blessingsName
          this.form.content = data.data.blessingsContent
        } else {
          this.$message({
            message: data.msg,
            type: 'error'
          })
        }
      })
    },
    getTempByBless (id) {
      getTemplateByBlessingsId({ blessingsId: id }).then(data => {
        if (data.result === 1) {
          this.checkedTempData = Object.entries(data.data).map(item => {
            return item[1].id
          })
        }
      })
    },
    submitAdd (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.ids = this.checkedTempData.join(',')
          addBless(this.form).then((data) => {
            if (data.result === 1) {
              setTimeout(() => {
                this.$router.back()
              }, 1000)
            }
          })
        } else {
          return false
        }
      })
    },
    submitEdit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.ids = this.checkedTempData.join(',')
          this.form.id = this.id
          editBless(this.form).then((data) => {
            if (data.result === 1) {
              setTimeout(() => {
                this.$router.back()
              }, 1000)
            }
          })
        } else {
          return false
        }
      })
    }
  },
  created () {
    this.id = this.$route.params.id
    this.oldPage = this.$route.params.page ? this.$route.params.page : ''
  },
  beforeMount () {
    this.getTempData()
    if (this.id) {
      this.getTempByBless(this.id)
    } this.getDetails(this.id)
  },
  mounted () {

  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#eee;
.table-div{
    width:90%;
    margin: 20px auto
}
.title{
    width:90%;
    margin: 10px auto;
    .search-div,.row-div{
      float: left;
    }
    .search-div{
      width: 200px;
      margin-right: 20px
    }
}
.el-upload-list__item{
  height:auto !important;
}
</style>
